<?php


namespace wechat\src;

require_once dirname(dirname(__FILE__)). '/lib/phpqrcode.php';

class Native extends SrcAbstract
{
    /**
     * 模式
     */
    public $model = 1;

    /**
     * 设置参数
     */
    public function __construct($options) {
        list($this->mchid, $this->mchkey) = $options;
    }

    /**
     * 二维码内容生成
     */
    public function generateContent($options){
        $nonceStr = wxRandom(32);
        if($this->model == 1){
            $params = [
                'appid' => $this->wechat->appid,
                'mch_id' => $this->mchid,
                'product_id' => $options['product_id'],
                'time_stamp' => time(),
                'nonce_str' => $nonceStr,
                // 'sign'=>$options['sign'],
            ];
            $options = array_merge($params, $options);
            $options['sign'] = $this->MakeSign($options, $this->mchkey);
            $this->content = "weixin://wxpay/bizpayurl?" . $this->ToUrlParamsa($options);
        }
        if($this->model == 2){
            $this->content = "扫码支付模式二开发中...";
        }
        return $this->content;
    }

    /**
     * 显示付款二维码
     */
    public function showQrcode($options){
        $content = $this->generateContent($options);
        header("Content-type: image/png");
        echo \QRcode::png($content);
        die;
    }
    /**
     * 回调验证方法
     * @return 订单信息
     */
    public function notify()
    {
        $xml = file_get_contents("php://input");
        $result = ArrayFromXml($xml);
        if (empty($result)) {
            $this->setError(103, "微信通知数据未取得");
            return false;
        }
        //签名验证
        if ($result['sign'] == $this->MakeSign($result, $this->mchkey)) {
            $this->setError(0, "验签成功");
            return $result;
        } else {
            $this->setError(103, "签名验证失败");
            return false;
        }
    }

}